home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 69.0 KB | 2,288 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UAppleEvents.cp
- // Copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UAPPLEEVENTS__
- #include "UAppleEvents.h"
- #endif
-
- // MacApp
-
- #ifndef __UBUSYCURSOR__
- #include "UBusyCursor.h"
- #endif
-
- #ifndef __UCOREERRORMGR__
- #include "UCoreErrorMgr.h"
- #endif
-
- #ifndef __UCOREUTILITIES__
- #include "UCoreUtilities.h"
- #endif
-
- #ifndef __UDISPATCHER__
- #include "UDispatcher.h"
- #endif
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
- #ifndef __UFILE__
- #include "UFile.h"
- #endif
-
- #ifndef __ULISTITERATOR__
- #include "UListIterator.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __USCRIPTING__
- #include "UScripting.h"
- #endif
-
- #ifndef __USTREAM__
- #include "UStream.h"
- #endif
-
- #ifndef __USUBSTITUTION__
- #include "USubstitution.h"
- #endif
-
- // Toolbox
-
- #ifndef __AEREGISTRY__
- #include <AERegistry.h>
- #endif
-
- #ifndef __PROCESSES__
- #include <Processes.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __SEGLOAD__
- #include <SegLoad.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- //========================================================================================
- // static allocations
- //========================================================================================
-
- AEIdleUPP TAppleEvent::fgIdleProc;
- AEFilterUPP TAppleEvent::fgFilterProc;
-
- CAEDesc CAEDesc::fgNullDesc;
-
- //========================================================================================
- // CLASS TAppleEvent
- //========================================================================================
- #undef Inherited
- #define Inherited TObject
-
- #pragma segment AEInitialize
- MA_DEFINE_CLASS_M1(TAppleEvent,
- Inherited);
-
- //----------------------------------------------------------------------------------------
- // TOSADispatcher::MakeCreateElementEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- TAppleEvent* TAppleEvent::MakeCreateElementEvent(AEAddressDesc& targetAddress,
- AESendMode sendMode,
- CAEDesc& targetSpecifier,
- DescType newElementType,
- DescType newElementPosition,
- CAEDesc& initialData,
- CAEDesc& initialProperties)
- {
- TAppleEvent * theEvent = new TAppleEvent;
- theEvent->IAppleEvent(kAECoreSuite, kAECreateElement, targetAddress, sendMode);
-
- CTempDesc insertionRecord;
- CTempDesc insertionLocation;
- CTempDesc emptyRecord;
-
- FailInfo fi;
- Try(fi)
- {
-
- // create the insertion record
- insertionRecord.CreateRecord();
- insertionRecord.PutKeyDesc(keyAEObject, targetSpecifier);
- insertionRecord.PutKeyEnum(keyAEPosition, newElementPosition);
- insertionRecord.CoerceDesc(typeInsertionLoc, insertionLocation);
-
- // add the insertion record to the apple event
- theEvent->WriteParameter(keyAEInsertHere, insertionLocation);
-
- // add the class of the item to be created to the event
- theEvent->WriteType(keyAEObjectClass, newElementType);
-
- emptyRecord.CreateRecord();
-
- // add the initial data to the event
- if (initialData.GetDataHandle())
- theEvent->WriteParameter(keyAEData, initialData);
- else
- theEvent->WriteParameter(keyAEData, emptyRecord);
-
- if (initialProperties.GetDataHandle())
- theEvent->WriteParameter(keyAEPropData, initialProperties);
- else
- theEvent->WriteParameter(keyAEPropData, emptyRecord);
-
- fi.Success();
- }
- else // recover
- {
- theEvent = (TAppleEvent *)FreeIfObject(theEvent);
- fi.ReSignal();
- }
-
- return theEvent;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent constructor
- //----------------------------------------------------------------------------------------
- #pragma segment AEInitialize
-
- TAppleEvent::TAppleEvent() :
- fSendingMode(kAENoReply),
- fPriority(kAENormalPriority),
- fTimeoutVal(kAEDefaultTimeout),
- fFreeMessage(FALSE)
- {
- fMessage.dataHandle = NULL;
-
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::IAppleEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment AEInitialize
-
- void TAppleEvent::IAppleEvent(const AEEventClass itsEventClass,
- const AEEventID itsEventID,
- const AEAddressDesc& itsAddress,
- long itsSendingMode)
- {
- AppleEvent theMessage;
-
- IObject();
-
- FailInfo fi;
- Try(fi)
- {
- FailOSErr(AECreateAppleEvent(itsEventClass, itsEventID, &itsAddress, kAutoGenerateReturnID, kAnyTransactionID, &theMessage));
- fi.Success();
- }
- else // Recover
- {
- Free();
- fi.ReSignal();
- }
-
- fMessage = theMessage;
- fSendingMode = itsSendingMode;
- fFreeMessage = TRUE; // We created it, we need to free it
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::InitializeFromMessage:
- //----------------------------------------------------------------------------------------
- #pragma segment AEInitialize
-
- void TAppleEvent::InitializeFromMessage(const AppleEvent& theMessage,
- Boolean freeMessage)
- {
- IObject();
-
- fMessage = theMessage;
- fFreeMessage = freeMessage;
-
- // Initialize the reply mode.
- DescType actualType;
- Size actualSize;
- short interactLevel = 0;
- if (AEGetAttributePtr(&theMessage, keyInteractLevelAttr, typeShortInteger, &actualType, (Ptr) & interactLevel, sizeof(short), &actualSize) == noErr)
- fSendingMode = (interactLevel & kAEReplyModeMask);
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment AEClose
-
- TAppleEvent::~TAppleEvent()
- {
- if (fFreeMessage)
- AEDisposeDesc(&fMessage);
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::Send:
- //----------------------------------------------------------------------------------------
- #pragma segment AESend
-
- TAppleEvent* TAppleEvent::Send()
- {
- TAppleEvent * resultEvent = NULL;
- FailOSErr(SendEvent(resultEvent));
-
- return resultEvent;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::SendEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment AESend
-
- OSErr TAppleEvent::SendEvent()
- {
- TAppleEvent * resultEvent = NULL;
- OSErr err = SendEvent(resultEvent);
- FreeIfObject(resultEvent);
-
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::SendEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment AESend
-
- OSErr TAppleEvent::SendEvent(TAppleEvent*& replyEvent)
- {
- AppleEvent reply;
-
- OSErr err = AECreateDesc(typeNull, NULL, 0, &reply);
- if (err != noErr)
- return err;
-
- // Debugging note:
- // If the application is sending this Apple Event to itself, you can follow it
- // through the AESend by setting breakpoints in the functions
- // TOSADispatcher::PreDispatchHandlerGlue and TOSADispatcher::DispatchHandlerGlue
- // in UScripting.cp.
-
- Boolean keepBusy;
- if (gBusyCursor)
- keepBusy = gBusyCursor->KeepCursorBusy(TRUE);
-
- err = AESend(&fMessage, &reply, fSendingMode, fPriority, fTimeoutVal, TAppleEvent::fgIdleProc, TAppleEvent::fgFilterProc);
-
- if (gBusyCursor)
- gBusyCursor->KeepCursorBusy(keepBusy);
-
- if ((err == noErr) && (GetSendingMode() & kAEReplyModeMask) == kAEWaitReply)
- {
- replyEvent = new TAppleEvent;
- replyEvent->InitializeFromMessage(reply, TRUE);
- }
- else
- AEDisposeDesc(&reply);
-
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::GetAddress:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- void TAppleEvent::GetAddress(AEAddressDesc& theAddress)
-
- {
- FailOSErr(AEGetAttributeDesc(&fMessage, keyAddressAttr, typeWildCard, &theAddress));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::GetProcessSerialNumber:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- void TAppleEvent::GetProcessSerialNumber(ProcessSerialNumber& thePSN)
-
- {
- DescType actualType;
- Size actualSize;
- FailOSErr(AEGetAttributePtr(&fMessage, keyAddressAttr, typeProcessSerialNumber, &actualType, &thePSN, sizeof(ProcessSerialNumber), &actualSize));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::GetReturnID:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- long TAppleEvent::GetReturnID()
- {
- DescType actualType;
- long returnID;
- long actualSize;
-
- FailOSErr(AEGetAttributePtr(&fMessage, keyReturnIDAttr, typeLongInteger, &actualType, &returnID, sizeof(returnID), &actualSize));
-
- return returnID;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::GetTransactionID:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- long TAppleEvent::GetTransactionID()
- {
- long transactionID;
- DescType actualType;
- long actualSize;
-
- return (AEGetAttributePtr(&fMessage, keyTransactionIDAttr, typeLongInteger, &actualType, &transactionID, sizeof(transactionID), &actualSize) == noErr) ? transactionID : 0;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::SetAddress:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- void TAppleEvent::SetAddress(const AEAddressDesc& theAddress)
- {
- FailOSErr(AEPutAttributeDesc(&fMessage, keyAddressAttr, &theAddress));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::SetPriority:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- void TAppleEvent::SetPriority(short thePriority)
- {
- fPriority = thePriority;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::SetReturnID:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- void TAppleEvent::SetReturnID(long returnID)
- {
- FailOSErr(AEPutAttributePtr(&fMessage, keyReturnIDAttr, typeLongInteger, &returnID, sizeof(returnID)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::SetSendingMode:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- void TAppleEvent::SetSendingMode(long sendingMode)
- {
- fSendingMode = sendingMode;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::SetTimeoutVal:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- void TAppleEvent::SetTimeoutVal(long theTimeoutVal)
- {
- fTimeoutVal = theTimeoutVal;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::SetTransactionID:
- //----------------------------------------------------------------------------------------
- #pragma segment AEAttributes
-
- void TAppleEvent::SetTransactionID(long transactionID)
- {
- FailOSErr(AEPutAttributePtr(&fMessage, keyTransactionIDAttr, typeLongInteger, &transactionID, sizeof(transactionID)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadEventClass:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- AEEventClass TAppleEvent::ReadEventClass()
- {
- DescType typeCode;
- AEEventClass eventClass;
- Size actualSize;
-
- FailOSErr(AEGetAttributePtr(&fMessage, keyEventClassAttr, typeType, &typeCode, &eventClass, sizeof(eventClass), &actualSize));
- return eventClass;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadEventClass:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- AEEventID TAppleEvent::ReadEventID()
- {
- DescType typeCode;
- AEEventID eventID;
- Size actualSize;
-
- FailOSErr(AEGetAttributePtr(&fMessage, keyEventIDAttr, typeType, &typeCode, &eventID, sizeof(eventID), &actualSize));
- return eventID;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadShort:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- short TAppleEvent::ReadShort(const AEKeyword theKey)
- {
- DescType actualType;
- Size actualSize;
- short theData = 0;
-
- FailOSErr(AEGetParamPtr(&fMessage, theKey, typeShortInteger, &actualType, &theData, sizeof(theData), &actualSize));
-
- return theData;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadLong:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- long TAppleEvent::ReadLong(const AEKeyword theKey)
- {
- DescType actualType;
- Size actualSize;
- long theData = 0;
-
- FailOSErr(AEGetParamPtr(&fMessage, theKey, typeLongInteger, &actualType, &theData, sizeof(theData), &actualSize));
-
- return theData;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadString:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::ReadString(const AEKeyword theKey,
- CStr255& theData)
- {
- DescType actualType;
- Size actualSize;
-
- theData.Empty();
- FailOSErr(AEGetParamPtr(&fMessage, theKey, typeChar, &actualType, &theData[1], sizeof(CStr255) - 1, &actualSize));
-
- if (actualSize > 255)
- {
- #if qDebug
- fprintf(stderr, "TAppleEvent::ReadString: String was longer than 255 bytes. Will be truncated\n");
- #endif
-
- actualSize = 255;
- }
- theData.Length() = (unsigned char)actualSize;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadSectionHandle:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- SectionHandle TAppleEvent::ReadSectionHandle(const AEKeyword theKey)
- {
- DescType actualType;
- Size actualSize;
- SectionHandle theData = NULL;
-
- FailOSErr(AEGetParamPtr(&fMessage, theKey, typeSectionH, &actualType, &theData, sizeof(SectionHandle), &actualSize));
-
- return theData;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadPtrList:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::ReadPtrList(const AEKeyword theKey,
- const DescType theType,
- TDynamicArray* theData)
- {
- AEDescList theDescList;
-
- FailOSErr(AEGetParamDesc(&fMessage, theKey, typeAEList, &theDescList));
-
- long items;
- AEKeyword theActualKey;
- DescType theActualType;
- Size theActualSize;
- MAVolatileInit(Ptr, thePtr, NULL);
-
- FailInfo fi;
- Try(fi)
- {
- FailOSErr(AECountItems(&theDescList, &items));// Get the number of elements in the list
- thePtr = NewPermPtr(theData->fElementSize);
-
- for (short index = 1; index <= items; ++index)
- {
- FailOSErr(AEGetNthPtr(&theDescList, index, theType, &theActualKey, &theActualType, thePtr, theData->fElementSize, &theActualSize));
-
- theData->InsertElementsBefore(theData->GetSize() + 1, thePtr, 1);
- }
- fi.Success();
- }
- else // Recover
- {
- AEDisposeDesc(&theDescList);
- thePtr = DisposeIfPtr(thePtr);
- fi.ReSignal();
- }
-
- AEDisposeDesc(&theDescList);
- thePtr = DisposeIfPtr(thePtr);
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadHandleList:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::ReadHandleList(const AEKeyword theKey,
- const DescType theType,
- TDynamicArray* theData)
- {
- AEDescList theDescList;
-
- FailOSErr(AEGetParamDesc(&fMessage, theKey, typeAEList, &theDescList));
-
- AEDesc theHandle;
- AEKeyword theActualKey;
- long items;
- FailInfo fi;
-
- Try(fi)
- {
- theHandle.dataHandle = NULL;
- FailOSErr(AECountItems(&theDescList, &items));// Get the number of elements in the list
- for (short index = 1; index <= items; ++index)
- {
- FailOSErr(AEGetNthDesc(&theDescList, index, theType, &theActualKey, &theHandle));
- theData->InsertElementsBefore(theData->GetSize() + 1, &theHandle.dataHandle, 1);
- theHandle.dataHandle = NULL;
- }
- fi.Success();
- }
- else
- {
- AEDisposeDesc(&theDescList);
- AEDisposeDesc(&theHandle);
- fi.ReSignal();
- }
- AEDisposeDesc(&theDescList);
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadParameter:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- OSErr TAppleEvent::ReadParameter(const AEKeyword theKey,
- const DescType desiredType,
- CAEDesc& theData)
- {
- return AEGetParamDesc(&fMessage, theKey, desiredType, theData);
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadParameterPtr:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::ReadParameterPtr(const AEKeyword theKey,
- const DescType desiredType,
- DescType& actualType,
- Ptr theData,
- long maximumSize,
- long& actualSize)
- {
- FailOSErr(AEGetParamPtr(&fMessage, theKey, desiredType, &actualType, theData, maximumSize, &actualSize));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteShort:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteShort(const AEKeyword theKey,
- short theData)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeShortInteger, &theData, sizeof(theData)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteLong:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteLong(const AEKeyword theKey,
- long theData)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeLongInteger, &theData, sizeof(theData)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteString:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteString(const AEKeyword theKey,
- const CStr255& theData)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeChar, &theData[1], theData.Length()));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteSectionHandle:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteSectionHandle(const AEKeyword theKey,
- SectionHandle theData)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeSectionH, &theData, sizeof(SectionHandle)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WritePtrList:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WritePtrList(const AEKeyword theKey,
- const DescType theType,
- TDynamicArray* theData)
- {
- CAEDesc theDescList; // used to be VOLATILE, but since it's a structure it won't be cached into registers on OUR compilers
-
- FailInfo fi;
- Try(fi)
- {
- theDescList.CreateList();
-
- {
- // Create a block for the iterator to disambiguate it from the failure handler it's nested in
- CArrayIterator iter(theData);
-
- for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
- FailOSErr(AEPutPtr(theDescList, (long)i, theType, theData->ComputeAddress(i), theData->fElementSize));
- }
- FailOSErr(AEPutParamDesc(&fMessage, theKey, theDescList));
- fi.Success();
- }
- else // Recover
- {
- FailOSErr(theDescList.DisposeDesc());
- fi.ReSignal();
- }
-
- FailOSErr(theDescList.DisposeDesc());
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteHandleList:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteHandleList(const AEKeyword theKey,
- const DescType theType,
- TDynamicArray* theData)
- {
- CAEDesc theDescList; // used to be VOLATILE, but since it's a structure it won't be cached into registers on OUR compilers
-
- FailInfo fi;
- Try(fi)
- {
- theDescList.CreateList();
-
- {
- // Create a block for the iterator to disambiguate it from the failure handler it's nested in
- CArrayIterator iter(theData);
-
- for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
- {
- CAEDesc theElement(theType, NULL);
- theElement.fAEDesc.dataHandle = *((Handle *)(theData->ComputeAddress(i)));
- theDescList.PutListItem((long)i, theElement);
- }
- }
-
- FailOSErr(AEPutParamDesc(&fMessage, theKey, theDescList));
- fi.Success();
- }
- else // Recover
- {
- FailOSErr(theDescList.DisposeDesc());
- fi.ReSignal();
- }
-
- FailOSErr(theDescList.DisposeDesc());
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteParameter:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteParameter(const AEKeyword theKey,
- const CAEDesc& theData)
- {
- FailOSErr(AEPutParamDesc(&fMessage, theKey, theData));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteParameterPtr:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteParameterPtr(const AEKeyword theKey,
- const DescType typeCode,
- Ptr theData,
- long dataSize)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeCode, theData, dataSize));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::DeleteParameter:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::DeleteParameter(const AEKeyword theKey)
- {
- FailOSErr(AEDeleteKeyDesc(&fMessage, theKey));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::CheckRequiredParameters:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::CheckRequiredParameters()
- {
- // look for the keyMissedKeywordAttr
- DescType returnedType;
- Size actualSize;
- OSErr theErr = AEGetAttributePtr(&fMessage, keyMissedKeywordAttr, typeWildCard, &returnedType, NULL, 0, &actualSize);
- if (theErr == noErr)
- {
- // We missed a parameter!
- FailOSErr(errAEParamMissed);
- }
- else if (theErr != errAEDescNotFound)
- {
- // Its OK if we got errAEDescNotFound because that means we got all the params.
- FailOSErr(theErr);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::HasParameter:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean TAppleEvent::HasParameter(const AEKeyword theKey)
- {
- DescType typeCode;
- long actualSize;
- return AEGetParamPtr(&fMessage, theKey, typeWildCard, &typeCode, NULL, 0, &actualSize) == noErr;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ParameterType:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- DescType TAppleEvent::ParameterType(const AEKeyword theKey)
- {
- // Returns the descriptor type of an event parameter.
- // TAppleEvent::ParameterType is here courtesy of Matt Clark. Thanks Matt.
- CTempDesc paramDesc;
-
- OSErr theErr = AEGetParamDesc(&fMessage, theKey, typeWildCard, paramDesc);
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
- DescType result = paramDesc.GetDescriptorType();
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadType:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- DescType TAppleEvent::ReadType(const AEKeyword theKey)
- {
- DescType actualType;
- long actualSize;
- DescType theData = 0;
-
- OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeType, &actualType, &theData, sizeof(theData), &actualSize);
-
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
-
- return theData;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadEnum:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- DescType TAppleEvent::ReadEnum(const AEKeyword theKey)
- {
- DescType actualType;
- long actualSize;
- DescType theData = 0;
-
- OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeEnumeration, &actualType, &theData, sizeof(theData), &actualSize);
-
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
-
- return theData;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadBoolean:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean TAppleEvent::ReadBoolean(const AEKeyword theKey)
- {
- DescType actualType;
- long actualSize;
- Boolean theData = FALSE;
-
- OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeBoolean, &actualType, &theData, sizeof(theData), &actualSize);
-
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
-
- return theData;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadRect:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::ReadRect(const AEKeyword theKey,
- CRect& theRect)
- {
- DescType actualType;
- long actualSize;
- CRect localRect;
-
- OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeQDRectangle, &actualType, &localRect, sizeof(localRect), &actualSize);
-
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
-
- theRect = localRect;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteType:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteType(const AEKeyword theKey,
- DescType theType)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeType, &theType, sizeof(theType)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteEnum:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteEnum(const AEKeyword theKey,
- DescType theEnum)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeEnumeration, &theEnum, sizeof(theEnum)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteBoolean:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteBoolean(const AEKeyword theKey,
- Boolean theBoolean)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeBoolean, &theBoolean, sizeof(theBoolean)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteRect:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteRect(const AEKeyword theKey,
- const CRect& theRect)
- {
- FailOSErr(AEPutParamPtr(&fMessage, theKey, typeQDRectangle, &theRect, sizeof(theRect)));
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteReplySpecifier:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOSLDispatch
-
- Boolean TAppleEvent::WriteReplySpecifier(MScriptableObject* theReplyObject)
- {
- // Use this whenever you need to return the object's specifier as the result of an event.
- // Makes an object specifier for an object and puts it in the keyAEResult parameter of this reply event.
- // MScriptableObject::ReturnReplySpecifier is here courtesy of Matt Clark. Thanks Matt.
- Boolean result = FALSE;
-
- if (fMessage.dataHandle)
- {
- CTempDesc objSpecifier;
- if (theReplyObject->MakeObjectSpecifier(objSpecifier, theReplyObject->GetSpecifierForm()))
- WriteParameter(keyAEResult, objSpecifier);
-
- result = TRUE;
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteError:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteError(long errNum,
- short errorStringResource,
- short errorStringIndex)
- {
- if (fMessage.dataHandle)
- {
- WriteLong(keyErrorNumber, errNum);
- CStr255 errStr;
- GetIndString(errStr, errorStringResource, errorStringIndex);
- WriteString(keyErrorString, errStr);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::WriteOSError
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::WriteOSError(OSErr error)
- {
- if (fMessage.descriptorType != typeNull && fMessage.dataHandle)
- {
- CStr255 errStr;
- WriteLong(keyErrorNumber, errAEEventFailed);
- if (!LookupErrString(error, errReasonID, errStr))
- {
- // No error string was found, substitute the generic one.
- CStr255 errNumStr;
- LookupErrString(errGenericErrorWithNum, errReasonID, errStr);
- NumToString(error, errNumStr);
- MAParamText("^ENUM", errNumStr);
- MAReplaceText(errStr);
- }
-
- WriteString(keyErrorString, errStr);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TAppleEvent::ReadError:
- //----------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void TAppleEvent::ReadError(long& errorNumber,
- CStr255& errorString)
- {
- errorNumber = noErr;
-
- if (HasParameter(keyErrorNumber))
- errorNumber = (OSErr)ReadLong(keyErrorNumber);
-
- if (HasParameter(keyErrorString))
- ReadString(keyErrorString, errorString);
- }
-
- //========================================================================================
- // CLASS CAEDesc
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // CAEDesc::CAEDesc:
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- CAEDesc::CAEDesc()
- {
- fAEDesc.descriptorType = typeNull;
- fAEDesc.dataHandle = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // CAEDesc::CAEDesc:
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- CAEDesc::CAEDesc(const AEDesc& theDesc) :
- fAEDesc(theDesc)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CAEDesc::CAEDesc:
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- CAEDesc::CAEDesc(const AEDesc* theDesc) :
- fAEDesc(*theDesc)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CAEDesc::CAEDesc:
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- CAEDesc::CAEDesc(DescType theType,
- Handle theHandle)
- {
- fAEDesc.dataHandle = NULL;
- SetDesc(theType, theHandle);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::CanCoerceDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::CanCoerceDesc(DescType toType) const
- {
- OSErr err;
- CTempDesc tempDesc;
- AEDesc tempDescriptor = fAEDesc; // use temp to enable function to be declared
- // const
-
- err = AECoerceDesc(&tempDescriptor, toType, tempDesc);
-
- return (err == noErr);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::CoerceDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::CoerceDesc(DescType toType,
- AEDesc* result) const
- {
- AEDesc tempDescriptor = fAEDesc; // use temp to enable function to be declared
- // const.
- FailOSErr(AECoerceDesc(&tempDescriptor, toType, result));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::CreateDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::CreateDesc(DescType typeCode,
- const void* dataPtr,
- Size dataSize)
- {
- if (fAEDesc.dataHandle) // optimization
- FailOSErr(DisposeDesc());
-
- FailOSErr(AECreateDesc(typeCode, dataPtr, dataSize, &fAEDesc));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::DisposeDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- OSErr CAEDesc::DisposeDesc()
- // DisposeDesc returns a status because it is used in destructors, where we can't fail,
- // and it is also used independently, as in CreateDesc.
- {
- OSErr result = noErr;
-
- if (fAEDesc.dataHandle)
- result = AEDisposeDesc(&fAEDesc);
-
- if (result == noErr)
- {
- fAEDesc.dataHandle = NULL;
- fAEDesc.descriptorType = typeNull;
- }
- return result;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyPtr:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- OSErr CAEDesc::GetKeyPtr(AEKeyword theAEKeyword,
- DescType desiredType,
- DescType* typeCode,
- void* dataPtr,
- Size maximumSize,
- Size* actualSize) const
- {
- AEDesc tempDescriptor = fAEDesc; // use temp to enable function to be const.
-
- return AEGetKeyPtr(&tempDescriptor, theAEKeyword, desiredType, typeCode, dataPtr, maximumSize, actualSize);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyPtr:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyPtr(AEKeyword theAEKeyword,
- DescType typeCode,
- const void* dataPtr,
- Size dataSize)
- {
- FailOSErr(AEPutKeyPtr(&fAEDesc, theAEKeyword, typeCode, dataPtr, dataSize));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::SetDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::SetDesc(DescType theType,
- Handle theHandle)
- {
- fAEDesc.descriptorType = theType;
- fAEDesc.dataHandle = theHandle;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::EqualDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::EqualDesc(const CAEDesc& theDesc) const
- {
- return CompareDesc(theDesc, kAEEquals);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::CompareDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::CompareDesc(const CAEDesc& theDesc,
- DescType compareHow) const
- {
- CAEDesc localDesc1 = *this;
- CAEDesc localDesc2 = theDesc;
- DescType type1 = localDesc1.GetDescriptorType();
- DescType type2 = localDesc2.GetDescriptorType();
- Boolean tossDesc1 = FALSE;
- Boolean tossDesc2 = FALSE;
- Boolean theResult = FALSE;
-
- if ((compareHow == kAEEquals) && (localDesc1.GetDataHandle() == localDesc2.GetDataHandle()))
- return TRUE;
-
- if (type1 != type2)
- {
- // Try coercing theDesc to type1
- if (AECoerceDesc(theDesc, type1, localDesc2) == noErr)
- tossDesc2 = TRUE;
- else
- // Try coercing myself to type2
- if (AECoerceDesc(*this, type2, localDesc1) == noErr)
- tossDesc1 = TRUE;
- else
- return FALSE;
- }
-
- type1 = localDesc1.GetDescriptorType();
- FailInfo fi;
- Try(fi)
- {
-
- switch (type1)
- {
- case typeAEList:
- {
- // Compare all the elements in the list
- long items1 = localDesc1.CountListItems();
- long items2 = localDesc2.CountListItems();
- if (((compareHow == kAEEquals) && (items1 == items2)) || (((compareHow == kAEBeginsWith) || (compareHow == kAEEndsWith)) && (items1 >= items2)))
- {
- theResult = TRUE; // empty lists are equal
- long numToCheck = items1;
- if ((compareHow == kAEBeginsWith) || (compareHow == kAEEndsWith))
- numToCheck = items2;
- long list1Indx = 1;
- if (compareHow == kAEEndsWith)
- list1Indx = (items1 - items2) + 1;
- for (long i = 1; i <= numToCheck; i++)
- {
- CTempDesc list1Item;
- CTempDesc list2Item;
- localDesc1.GetNthItem(list1Indx++, typeWildCard, list1Item);
- localDesc2.GetNthItem(i, typeWildCard, list2Item);
- theResult &= list1Item.CompareDesc(list2Item, kAEEquals);
- }
- }
- else
- theResult = FALSE;
- break;
- }
-
- case typeChar:
- {
- Handle charsHdl1 = localDesc1.GetDataHandle();
- Handle charsHdl2 = localDesc2.GetDataHandle();
- SignedByte was1State = LockHandle(charsHdl1);
- SignedByte was2State = LockHandle(charsHdl2);
- short size1 = (short)localDesc1.GetDataSize();
- short size2 = (short)localDesc2.GetDataSize();
- short magIDResult = IUMagString(*charsHdl1, *charsHdl2, size1, size2);
- switch (compareHow)
- {
- case kAEGreaterThan:
- theResult = (magIDResult == 1);
- break;
- case kAEGreaterThanEquals:
- theResult = (magIDResult != -1);
- break;
- case kAEEquals:
- theResult = (magIDResult == 0);
- break;
- case kAELessThan:
- theResult = (magIDResult == -1);
- break;
- case kAELessThanEquals:
- theResult = (magIDResult != 1);
- break;
- case kAEBeginsWith:
- {
- if (size1 >= size2)
- theResult = (IUMagString(*charsHdl1, *charsHdl2, size2, size2) == 0);
- break;
- }
- case kAEEndsWith:
- {
- long theOffset = size1 - size2;
- if (theOffset >= 0)
- theResult = (IUMagString(*charsHdl1 + theOffset, *charsHdl2, size2, size2) == 0);
- break;
- }
- case kAEContains:
- {
- if (size1 >= size2)
- theResult = (Munger(charsHdl1, 0, *charsHdl2, size2, NULL, 0) >= 0);
- break;
- }
- }
- HSetState(charsHdl1, was1State);
- HSetState(charsHdl2, was2State);
-
- break;
- }
-
- // Compare rectangles
- case typeQDRectangle:
- {
- CRect rect1;
- localDesc1.GetRect(rect1);
- CRect rect2;
- localDesc2.GetRect(rect2);
- if (compareHow == kAEEquals)
- theResult = rect1 == rect2;
- else if (compareHow == kAEContains)
- theResult = rect1.Contains(rect2);
- break;
- }
-
- // Compare colors
- case typeRGBColor:
- if (compareHow == kAEEquals)
- {
- CRGBColor myPropColor;
- localDesc1.GetRGBColor(myPropColor);
- CRGBColor compareColor;
- localDesc2.GetRGBColor(compareColor);
- theResult = myPropColor == compareColor;
- break;
- }
-
- case typeLongDateTime:
- {
- unsigned long num1 = 0;
- unsigned long num2 = 0;
- num1 = (unsigned long)localDesc1.GetDateTime();
- num2 = (unsigned long)localDesc2.GetDateTime();
- switch (compareHow)
- {
- case kAEGreaterThan:
- theResult = num1 > num2;
- break;
- case kAEGreaterThanEquals:
- theResult = num1 >= num2;
- break;
- case kAEEquals:
- theResult = num1 == num2;
- break;
- case kAELessThan:
- theResult = num1 < num2;
- break;
- case kAELessThanEquals:
- theResult = num1 <= num2;
- break;
- }
- }
- break;
-
- // Handle all the types we can convert to longs and then compare
- case typeType:
- case typeEnumerated:
- case typeBoolean:
- case typeLongInteger:
- case typeShortInteger:
- {
- long num1 = 0;
- long num2 = 0;
-
- switch (type1)
- {
- // There are no coercion handlers for some of these types so we'll get them by
- // the descriptor type then cast the results to longs.
- case typeEnumerated:
- {
- num1 = (long)localDesc1.GetEnum();
- num2 = (long)localDesc2.GetEnum();
- break;
- }
- case typeType:
- {
- num1 = (long)localDesc1.GetType();
- num2 = (long)localDesc2.GetType();
- break;
- }
- case typeBoolean:
- {
- num1 = (long)localDesc1.GetBoolean();
- num2 = (long)localDesc2.GetBoolean();
- break;
- }
- default:
- {
- num1 = localDesc1.GetLong();
- num2 = localDesc2.GetLong();
- }
- }
- switch (compareHow)
- {
- case kAEGreaterThan:
- theResult = num1 > num2;
- break;
- case kAEGreaterThanEquals:
- theResult = num1 >= num2;
- break;
- case kAEEquals:
- theResult = num1 == num2;
- break;
- case kAELessThan:
- theResult = num1 < num2;
- break;
- case kAELessThanEquals:
- theResult = num1 <= num2;
- break;
- }
- break;
- }
-
- default:
- {
- // Check for the same handle content
- long size1 = localDesc1.GetDataSize();
- long size2 = localDesc2.GetDataSize();
- Handle data1 = localDesc1.GetDataHandle();
- Handle data2 = localDesc2.GetDataHandle();
- theResult = ((compareHow == kAEEquals) && (size1 == size2) && (EqualBlocks(*data1, *data2, (short)size1)));
- }
- }
- fi.Success();
- }
- else
- {
- // Don't resignal failure, just return false
- theResult = FALSE;
- }
- if (tossDesc1)
- localDesc1.DisposeDesc();
- if (tossDesc2)
- localDesc2.DisposeDesc();
- return theResult;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetDataSize:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- long CAEDesc::GetDataSize() const
- {
- return fAEDesc.dataHandle ? ::GetHandleSize(fAEDesc.dataHandle) : 0;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::CreateList:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::CreateList()
- {
- FailOSErr(AECreateList(NULL, 0, FALSE, &fAEDesc));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::CountListItems:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- long CAEDesc::CountListItems()
- {
- long theCount = 0;
- FailOSErr(AECountItems(&fAEDesc, &theCount));
-
- return theCount;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetNthItem:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::GetNthItem(long index,
- DescType desiredType,
- CAEDesc& theDesc)
- {
- DescType theAEKeyword;
- FailOSErr(AEGetNthDesc(&fAEDesc, index, desiredType, &theAEKeyword, theDesc));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutListItem:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutListItem(long index,
- CAEDesc& theDesc)
- {
- FailOSErr(AEPutDesc(&fAEDesc, index, theDesc));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::DeleteListItem:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- OSErr CAEDesc::DeleteListItem(long index)
- {
- return AEDeleteItem(&fAEDesc, index);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::CreateRecord:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::CreateRecord()
- {
- FailOSErr(AECreateList(NULL, 0, TRUE, &fAEDesc));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetFSSpec: Note that this method provides transparent coercion of alias records into
- // FSSpec records.
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::GetFSSpec(FSSpec& fileSpec) const
- {
- if (fAEDesc.descriptorType == typeFSS)
- fileSpec = **(FSSpec * *)fAEDesc.dataHandle;
- else
- {
- CTempDesc whichPropertyDesc;
- CoerceDesc(typeFSS, whichPropertyDesc);
- fileSpec = **(FSSpec * *)whichPropertyDesc.fAEDesc.dataHandle;
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutFSSpec:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutFSSpec(const FSSpec& fileSpec)
- {
- CreateDesc(typeFSS, &fileSpec, sizeof(fileSpec));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetRect:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::GetRect(CRect& theRect) const
- {
- if (fAEDesc.descriptorType == typeQDRectangle)
- theRect = **(CRect * *)fAEDesc.dataHandle;
- else
- {
- CTempDesc whichPropertyDesc;
- CoerceDesc(typeQDRectangle, whichPropertyDesc);
- theRect = **(CRect * *)whichPropertyDesc.fAEDesc.dataHandle;
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutRect:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutRect(const CRect& theRect)
- {
- CreateDesc(typeQDRectangle, &theRect, sizeof(theRect));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetType:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- DescType CAEDesc::GetType() const
- {
- DescType result;
- OSErr err = 0;
- if (fAEDesc.descriptorType == typeType)
- result = **(DescType * *)fAEDesc.dataHandle;
- else
- {
- CTempDesc whichPropertyDesc;
- CoerceDesc(typeType, whichPropertyDesc);
- result = **(DescType * *)whichPropertyDesc.fAEDesc.dataHandle;
- }
-
- return result;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutType:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutType(DescType theType)
- {
- CreateDesc(typeType, &theType, sizeof(theType));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetString:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::GetString(CStr255& theString,
- long maximumSize) const
- {
- long theSize = Min(GetDataSize(), maximumSize);
- if (theSize)
- {
- if (fAEDesc.descriptorType == typeChar)
- theString.CopyFrom((void*) * fAEDesc.dataHandle, theSize);
- else
- {
- CTempDesc myTempDesc;
- CoerceDesc(typeChar, myTempDesc);
-
- theString.CopyFrom((void*) * myTempDesc.fAEDesc.dataHandle, Min(maximumSize, myTempDesc.GetDataSize()));
- }
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutString:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutString(const CStr255& theString)
- {
- CreateDesc(typeChar, &theString.fStr[1], theString.Length());
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetBoolean:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetBoolean() const
- {
- Boolean result;
- if (fAEDesc.descriptorType == typeBoolean)
- result = **(Boolean * *)fAEDesc.dataHandle;
- else
- {
- CTempDesc myTempDesc;
- CoerceDesc(typeBoolean, myTempDesc);
- result = **(Boolean * *)myTempDesc.fAEDesc.dataHandle;
- }
-
- return result;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutBoolean:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutBoolean(Boolean theBoolean)
- {
- CreateDesc(typeBoolean, &theBoolean, sizeof(theBoolean));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetEnum:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- DescType CAEDesc::GetEnum() const
- {
- DescType result;
- if (fAEDesc.descriptorType == typeEnumeration)
- result = **(DescType * *)fAEDesc.dataHandle;
- else
- {
- CTempDesc myTempDesc;
- CoerceDesc(typeEnumeration, myTempDesc);
- result = **(DescType * *)myTempDesc.fAEDesc.dataHandle;
- }
-
- return result;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutEnum:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutEnum(DescType theEnum)
- {
- CreateDesc(typeEnumeration, &theEnum, sizeof(theEnum));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetLong:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- long CAEDesc::GetLong() const
- {
- long result;
- if (fAEDesc.descriptorType == typeLongInteger)
- result = **(long**)fAEDesc.dataHandle;
- else
- {
- CTempDesc myTempDesc;
- CoerceDesc(typeLongInteger, myTempDesc);
- result = **(long**)myTempDesc.fAEDesc.dataHandle;
- }
-
- return result;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutLong:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutLong(long theLong)
- {
- CreateDesc(typeLongInteger, &theLong, sizeof(theLong));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetDateTime:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- unsigned long CAEDesc::GetDateTime() const
- {
- LongDateTime date;
- ((LongDateCvt *) & date)->hl.lHigh = 0;
- ((LongDateCvt *) & date)->hl.lLow = 0;
-
- if (fAEDesc.descriptorType == typeLongDateTime)
- date = **(LongDateTime * *)fAEDesc.dataHandle;
- else
- {
- CTempDesc myTempDesc;
- CoerceDesc(typeLongDateTime, myTempDesc);
- date = **(LongDateTime * *)myTempDesc.fAEDesc.dataHandle;
- }
-
- return ((LongDateCvt *) & date)->hl.lLow;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutDateTime:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutDateTime(unsigned long theDateTime)
- {
- LongDateTime date;
- ((LongDateCvt *) & date)->hl.lHigh = 0;
- ((LongDateCvt *) & date)->hl.lLow = theDateTime;
- CreateDesc(typeLongDateTime, &date, sizeof(date));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetRGBColor:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::GetRGBColor(CRGBColor& theColor) const
- {
- if (fAEDesc.descriptorType == typeRGBColor)
- theColor = **(CRGBColor * *)fAEDesc.dataHandle;
- else
- {
- CTempDesc myTempDesc;
- CoerceDesc(typeRGBColor, myTempDesc);
- theColor = **(CRGBColor * *)myTempDesc.fAEDesc.dataHandle;
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutRGBColor:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutRGBColor(const CRGBColor& theColor)
- {
- CreateDesc(typeRGBColor, &theColor, sizeof(theColor));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetObject:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- MScriptableObject* CAEDesc::GetObject() const
- {
- MScriptableObject * theResult;
- if (fAEDesc.dataHandle)
- theResult = **(MScriptableObject * **)fAEDesc.dataHandle;
- else
- theResult = gDispatcher;
-
- return theResult;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutObject:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutObject(MScriptableObject* theObject)
- {
- CreateDesc(theObject->GetOMClass(), &theObject, sizeof(theObject));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyString:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetKeyString(DescType theKey,
- CStr255& theStr,
- long maximumSize) const
- {
- CStr255 localString;
- DescType actualType;
- long actualSize = 0;
- OSErr iErr = GetKeyPtr(theKey, typeChar, &actualType, &localString[1], maximumSize, &actualSize);
- if (iErr == 0)
- {
- localString[0] = (unsigned char)actualSize;
- theStr = localString;
- }
-
- return (iErr == 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyEnum:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetKeyEnum(DescType theKey,
- DescType& theEnum) const
- {
- DescType localEnum;
- DescType actualType;
- long actualSize = 0;
- OSErr iErr = GetKeyPtr(theKey, typeEnumeration, &actualType, &localEnum, 4, &actualSize);
- if (iErr == 0)
- theEnum = localEnum;
-
- return (iErr == 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetKeyDesc(DescType theKey,
- DescType desiredType,
- CAEDesc& theAEDesc) const
- {
- CAEDesc localDesc;
-
- AEDesc tempDescriptor = fAEDesc; // temp to allow function to be const.
-
- OSErr iErr = AEGetKeyDesc(&tempDescriptor, theKey, desiredType, localDesc);
- if (iErr == 0)
- theAEDesc = localDesc;
-
- return (iErr == 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyLong:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetKeyLong(DescType theKey,
- long& theNum) const
- {
- long localNum;
- DescType actualType;
- long actualSize = 0;
- OSErr iErr = GetKeyPtr(theKey, typeLongInteger, &actualType, &localNum, 4, &actualSize);
- if (iErr == 0)
- theNum = localNum;
-
- return (iErr == 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyRect:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetKeyRect(DescType theKey,
- CRect& theRect) const
- {
- CRect localRect;
- DescType actualType;
- long actualSize = 0;
- OSErr iErr = GetKeyPtr(theKey, typeQDRectangle, &actualType, &localRect, sizeof(localRect), &actualSize);
- if (iErr == 0)
- theRect = localRect;
-
- return (iErr == 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyDateTime:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetKeyDateTime(DescType theKey,
- unsigned long& theDate) const
- {
- LongDateTime date;
- ((LongDateCvt *) & date)->hl.lHigh = 0;
- ((LongDateCvt *) & date)->hl.lLow = 0;
- DescType actualType;
- long actualSize = 0;
- OSErr iErr = GetKeyPtr(theKey, typeLongDateTime, &actualType, &date, sizeof(date), &actualSize);
- if (iErr == noErr)
- theDate = ((LongDateCvt *) & date)->hl.lLow;
-
- return (iErr == 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyBoolean:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetKeyBoolean(DescType theKey,
- Boolean& theBoolean) const
- {
- Boolean localBoolean;
- DescType actualType;
- long actualSize = 0;
- OSErr iErr = GetKeyPtr(theKey, typeBoolean, &actualType, &localBoolean, sizeof(localBoolean), &actualSize);
- if (iErr == noErr)
- theBoolean = localBoolean;
-
- return (iErr == 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::GetKeyColor:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- Boolean CAEDesc::GetKeyColor(DescType theKey,
- CRGBColor& theColor) const
- {
- CRGBColor localColor;
- DescType actualType;
- long actualSize = 0;
- OSErr iErr = GetKeyPtr(theKey, typeRGBColor, &actualType, &localColor, sizeof(localColor), &actualSize);
- if (iErr == 0)
- theColor = localColor;
-
- return (iErr == 0);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyString:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyString(DescType theKey,
- const CStr255& theStr)
- {
- CStr255 localString = theStr;
- PutKeyPtr(theKey, typeChar, &localString[1], localString.Length());
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyEnum:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyEnum(DescType theKey,
- DescType theEnum)
- {
- PutKeyPtr(theKey, typeEnumeration, &theEnum, 4);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyDateTime:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyDateTime(DescType theKey,
- unsigned long theDate)
- {
- LongDateTime date;
- ((LongDateCvt *) & date)->hl.lHigh = 0;
- ((LongDateCvt *) & date)->hl.lLow = theDate;
- PutKeyPtr(theKey, typeLongDateTime, &date, sizeof(date));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::DeleteKeyDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- OSErr CAEDesc::DeleteKeyDesc(DescType theKey)
- {
- return AEDeleteKeyDesc(&fAEDesc, theKey);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyDesc(DescType theKey,
- CAEDesc& theAEDesc)
- {
- FailOSErr(AEPutKeyDesc(&fAEDesc, theKey, theAEDesc));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyLong:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyLong(DescType theKey,
- long theNum)
- {
- PutKeyPtr(theKey, typeLongInteger, &theNum, sizeof(theNum));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyRect:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyRect(DescType theKey,
- const CRect& theRect)
- {
- PutKeyPtr(theKey, typeQDRectangle, &theRect, sizeof(theRect));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyBoolean:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyBoolean(DescType theKey,
- Boolean theBoolean)
- {
- PutKeyPtr(theKey, typeBoolean, &theBoolean, sizeof(theBoolean));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::PutKeyColor:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::PutKeyColor(DescType theKey,
- const CRGBColor& theColor)
- {
- PutKeyPtr(theKey, typeRGBColor, &theColor, sizeof(theColor));
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::operator=:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- CAEDesc& CAEDesc::operator=(const CAEDesc& theDesc)
- {
- fAEDesc = theDesc.fAEDesc;
- return *this;
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::ReadFrom
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::ReadFrom(TStream* aStream)
- {
- FailOSErr(DisposeDesc());
- fAEDesc.descriptorType = (DescType)aStream->ReadLong();
- Boolean hasHandle = aStream->ReadBoolean();
- if (hasHandle)
- fAEDesc.dataHandle = aStream->ReadHandle();
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::ReadFrom
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::ReadFrom(TFile* aFile,
- ResType resourceType,
- short resourceID)
- {
- #if qDebug
- FailNonObject(aFile);
- #endif
-
- FailOSErr(DisposeDesc());
- if (aFile->HasRsrcFork())
- {
- Handle dataHandle;
-
- short savedRefNum = aFile->UseResource();
- dataHandle = Get1Resource(resourceType, resourceID);
- MAUseResFile(savedRefNum);
-
- if (dataHandle)
- {
- fAEDesc.descriptorType = resourceType;
- DetachResource(dataHandle);
- fAEDesc.dataHandle = dataHandle;
- }
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::WriteTo
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::WriteTo(TStream* aStream)
- {
- aStream->WriteLong((long)GetDescriptorType());
- Handle theDataHandle = GetDataHandle();
- aStream->WriteBoolean(theDataHandle != NULL);
- if (theDataHandle)
- aStream->WriteHandle(theDataHandle);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CAEDesc::WriteTo
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEReadWrite
-
- void CAEDesc::WriteTo(TFile* aFile,
- short resourceID,
- CStr255& resourceName)
- {
- #if qDebug
- FailNonObject(aFile);
- #endif
-
- if (aFile->HasRsrcFork() && fAEDesc.dataHandle != NULL)
- {
- Handle dataHandle = fAEDesc.dataHandle; // not volatile...passed by address
-
- FailInfo fi;
- Try(fi)
- {
- FailOSErr(HandToHand(&dataHandle));
-
- short savedResFile = aFile->UseResource();
- MAAddResource(dataHandle, fAEDesc.descriptorType, resourceID, resourceName);
- FailResError();
- WriteResource(dataHandle);
- ReleaseResource(dataHandle);
-
- MAUseResFile(savedResFile);
- fi.Success();
- }
- else // recover
- {
- DisposeIfHandle(dataHandle);
- fi.ReSignal();
- }
- }
- }
-
- //========================================================================================
- // CLASS CTempDesc
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // CTempDesc::CTempDesc:
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- CTempDesc::CTempDesc()
- {
- fFailInfo.SetCleanupProc(CTempDesc::CallCleanup, this);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CTempDesc::CTempDesc
- //--------------------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- CTempDesc::CTempDesc(const AEDesc& theDesc) :
- CAEDesc(theDesc)
- {
- fFailInfo.SetCleanupProc(CTempDesc::CallCleanup, this);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CTempDesc::CTempDesc
- //--------------------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- CTempDesc::CTempDesc(DescType theType,
- Handle theHandle) :
- CAEDesc(theType, theHandle)
- {
- fFailInfo.SetCleanupProc(CTempDesc::CallCleanup, this);
- }
-
- //--------------------------------------------------------------------------------------------------
- // CTempDesc::~CTempDesc:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEClose
-
- CTempDesc::~CTempDesc()
- {
- //Cleanup();
- if (fAEDesc.dataHandle) // optimization
- DisposeDesc();
-
- fAEDesc.descriptorType = typeNull;
- fAEDesc.dataHandle = NULL;
-
- fFailInfo.Success();
- }
-
- //--------------------------------------------------------------------------------------------------
- // CTempDesc::Cleanup:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEClose
-
- void CTempDesc::Cleanup()
- {
- if (fAEDesc.dataHandle) // optimization
- DisposeDesc();
- }
-
- //--------------------------------------------------------------------------------------------------
- // CTempDesc::CallCleanup:
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEClose
-
- /*static*/
- void CTempDesc::CallCleanup(void* context)
- {
- //((CTempDesc*) context)->Cleanup();
- ((CTempDesc *)context)->DisposeDesc(); // optimization
- }
-
- //----------------------------------------------------------------------------------------
- // End of UAppleEvents.cp
-
- #pragma segment Inline
-
-